package com.sc.nft.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.enums.MarketLevelEnum;
import com.sc.nft.service.DaoMoneyPoolService;
import com.sc.nft.service.UserMarketValueService;
import com.google.common.collect.Maps;
import com.sc.nft.service.UserPerformanceService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class    RecordTask {

    private final UserMarketValueService userMarketValueService;
    private final UserPerformanceService userPerformanceService;
    private final OrderDao orderDao;
    private final SecondOrderDao secondOrderDao;
    private final UserCityServerCentreDao userCityServerCentreDao;
    private final UserWorkRoomDao userWorkRoomDao;
    private final FirstOrderSettleDao firstOrderSettleDao;
    private final SecondOrderSettleDao secondOrderSettleDao;
    private final TradingPoolOrderSettleDao tradingPoolOrderSettleDao;
    private final DaoMoneyPoolService daoMoneyPoolService;
    public static final String DATE_FORMAT="yyyyMMdd";

    /**
     * 初始化用户昨天市值
     */
    @XxlJob("initMarketValue")
    public void initMarketValue() throws Exception {
        userMarketValueService.initMarketValue();
    }
    /**
     * 计算业绩
     */
    @XxlJob("calPerformance")
    public void calPerformance() throws Exception {

        Map<Long,Integer> map = Maps.newHashMap();
        for (UserWorkRoom userWorkRoom : userWorkRoomDao.list()) {
            map.put(userWorkRoom.getUserId(),1);
        }
        for (UserCityServerCentre userCityServerCentre : userCityServerCentreDao.list()) {
            map.put(userCityServerCentre.getUserId(),2);
        }
        for (Order order : orderDao.getWithSettlePerformance()) {
            String date = DateUtil.format(order.getOrderTime(),DATE_FORMAT);
            userPerformanceService.calFirstOrder(order,date,map);
        }
        for (SecondOrder order : secondOrderDao.getWithSettlePerformance()) {
            String date = DateUtil.format(order.getDealTime(),DATE_FORMAT);
            userPerformanceService.calSecondOrder(order,date,map);
        }

    }

    /**
     * 分配资金池
     */
    @XxlJob("shareMoneyPool")
    public void shareMoneyPool()  {
        DateTime nextWeek = DateUtil.beginOfWeek(DateUtil.nextWeek());
        String yearWeek =  String.valueOf(nextWeek.getField(DateField.YEAR))+String.valueOf(DateUtil.weekOfYear(nextWeek));
        for (FirstOrderSettle firstOrderSettle : firstOrderSettleDao.getWaitSettle()) {
            try{
                daoMoneyPoolService.shareMoney(NumberUtil.parseInt(yearWeek), MarketLevelEnum.FIRST,firstOrderSettle.getId(),firstOrderSettle.getVestId(),firstOrderSettle.getAllocationType(),firstOrderSettle.getAllocationAmount());
            }catch (Exception e){
                log.error("首发订单{}资金入池异常",firstOrderSettle.getOrderId(),e);
            }
        }
        for (SecondOrderSettle secondOrderSettle : secondOrderSettleDao.getWaitSettle()) {
            try{
                daoMoneyPoolService.shareMoney(NumberUtil.parseInt(yearWeek), MarketLevelEnum.SECOND,secondOrderSettle.getId(),secondOrderSettle.getVestId(),secondOrderSettle.getAllocationType(),secondOrderSettle.getAllocationAmount());
            }catch (Exception e){
                log.error("二级订单{}资金入池异常",secondOrderSettle.getOrderId(),e);
            }
        }
        for (TradingPoolOrderSettle tradingPoolOrderSettle : tradingPoolOrderSettleDao.getWaitSettle()) {
            try{
                daoMoneyPoolService.shareMoney(NumberUtil.parseInt(yearWeek), MarketLevelEnum.TRANS_POOL,tradingPoolOrderSettle.getId(),tradingPoolOrderSettle.getVestId(),tradingPoolOrderSettle.getAllocationType(),tradingPoolOrderSettle.getAllocationAmount());
            }catch (Exception e){
                log.error("交易池订单{}资金入池异常",tradingPoolOrderSettle.getOrderId(),e);
            }
        }
    }


}
